package com.linktel.productionLine.controller;

import com.linktel.productionLine.common.BaseResponse;
import com.linktel.productionLine.common.ErrorCode;
import com.linktel.productionLine.common.ResultUtils;
import com.linktel.productionLine.entity.dto.commitUser.CommitUserDTO;
import com.linktel.productionLine.entity.query.user.*;
import com.linktel.productionLine.exception.BusinessException;
import com.linktel.productionLine.service.CommitUserService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.util.List;

import static com.linktel.productionLine.constant.CommonConstant.CURRENT_USER;

/**
 * 对提交人员的有关请求
 *
 * @Author HuangJiaHui
 * @Date 2023/8/30 15:32
 */
@RestController
@RequestMapping("/user")
@AllArgsConstructor
public class CommitUserController {

    private CommitUserService commitUserService;

    /**
     * 登录
     *
     * @param userLoginQuery 登录信息封装
     * @param request 客户端请求对象
     * @return 是否登录成功
     */
    @PostMapping("/login")
    public BaseResponse<Boolean> userLogin(@RequestBody UserLoginQuery userLoginQuery, HttpServletRequest request) {
        String username = userLoginQuery.getUsername();
        String password = userLoginQuery.getPassword();
        if (username == null || password == null || "".equals(username) || "".equals(password)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        CommitUserDTO commitUser = commitUserService.userLogin(username, password);
        if (commitUser == null) {
            return ResultUtils.success(false);
        }
        request.getSession().setAttribute(CURRENT_USER, commitUser);
        return ResultUtils.success(true);
    }

    /**
     * 用户修改密码
     *
     * @param editUserPasswordQuery 修改密码封装
     * @param request 客户端请求对象
     * @return 是否修改成功
     */
    @PostMapping("/editPassword")
    public BaseResponse<Boolean> editUserPassword(@RequestBody EditUserPasswordQuery editUserPasswordQuery, HttpServletRequest request) {
        if (editUserPasswordQuery == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Boolean result = commitUserService.editUserPassword(editUserPasswordQuery);
        if (result) {
            request.getSession().removeAttribute(CURRENT_USER);
        }
        return ResultUtils.success(result);
    }

    /**
     * 退出登录
     *
     * @param request 客户端请求对象
     * @return 是否登出成功
     */
    @PostMapping("/logout")
    public BaseResponse<Boolean> userLogout(HttpServletRequest request) {
        request.getSession().removeAttribute(CURRENT_USER);
        return ResultUtils.success(true);
    }

    /**
     * 获取当前登录用户信息
     *
     * @param request 客户端请求对象
     * @return 当前登录用户信息
     */
    @PostMapping("/current")
    public BaseResponse<CommitUserDTO> getCurrentUser(HttpServletRequest request) {
        HttpSession session = request.getSession();
        CommitUserDTO commitUser = (CommitUserDTO) session.getAttribute(CURRENT_USER);
        return ResultUtils.success(commitUser);
    }

    /**
     * 得到所有用户信息
     *
     * @return 用户信息列表
     */
    @GetMapping("/list")
    public BaseResponse<List<CommitUserDTO>> listCommitUser() {
        List<CommitUserDTO> commitUserDTOList = commitUserService.listCommitUser();
        return ResultUtils.success(commitUserDTOList);
    }


    /**
     * 根据用户id得到该用户信息
     *
     * @param commitUserId 用户id
     * @return 该用户信息
     */
    @GetMapping("/getById")
    public BaseResponse<CommitUserDTO> getById(Long commitUserId) {
        if (commitUserId == null || commitUserId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        CommitUserDTO commitUserDTO = commitUserService.getById(commitUserId);
        return ResultUtils.success(commitUserDTO);
    }

    /**
     * 新增用户
     *
     * @param addCommitUserQuery 新增用户信息请求封装
     * @return 是否新增成功
     */
    @PostMapping
    public BaseResponse<Boolean> addCommitUser(@RequestBody AddCommitUserQuery addCommitUserQuery) {
        if (addCommitUserQuery == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Boolean result = commitUserService.addCommitUser(addCommitUserQuery);
        return ResultUtils.success(result);
    }

    /**
     * 修改用户
     *
     * @param editCommitUserQuery 更新用户信息请求封装
     * @return 是否修改成功
     */
    @PutMapping
    public BaseResponse<Boolean> editCommitUser(@RequestBody EditCommitUserQuery editCommitUserQuery) {
        if (editCommitUserQuery == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Boolean result = commitUserService.editCommitUser(editCommitUserQuery);
        return ResultUtils.success(result);
    }

    /**
     * 删除用户
     *
     * @param deleteCommitUserQuery 删除用户信息请求封装
     * @return 是否删除成功
     */
    @DeleteMapping
    public BaseResponse<Boolean> deleteCommitUser(@RequestBody DeleteCommitUserQuery deleteCommitUserQuery) {
        if (deleteCommitUserQuery == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Boolean result = commitUserService.deleteCommitUser(deleteCommitUserQuery);
        return ResultUtils.success(result);
    }

    //TODO 修改密码

}
